<?php

class BPlugin_session extends BPlugin
{
	
    /**
     * session保存方式：默认
     *
     */
    const FLAG_DEFAULT = 'default';

    /**
     * session保存方式：文件缓存
     *
     */
    const FLAG_FILECACHE = 'filecache';
    
    /**
     * session保存方式：内存缓存
     *
     */
    const FLAG_MEMCACHED = 'memcached';

    /**
     * 缓存对象
     * 
     * @access public
     * @var object
     */
    private $_container;

	/**
	 * 过期时间
	 * 
	 * @access private
	 * @var int
	 */
	private $_expire = 10800;

	/**
	 * 保存方式
	 * 
	 * @access private
	 * @var string
	 */
    private $_save_handler = self::FLAG_FILECACHE;

    /**
     * 构造器
     *
     */
    function __construct() {} 

    /**
     * 存取会话信息保存方式
     * 
     * @access public
     * @param string $handler
     * @return mixed
     */
    function saveHandler($handler = null)
    {
        if (is_null($handler)) {
            return $this->_save_handler;
        } elseif (!in_array($handler, array(self::FLAG_DEFAULT, self::FLAG_FILECACHE))) {
            throw new BException('params_unsupported', $handler);
        }
        $this->_save_handler =  $handler;
        return $this;
    }
	
	/**
	 * 配置session设置
	 * 
     * @access public
	 * @return bool
	 */
	function start($session_id = null)
	{
        if ($this->_save_handler != self::FLAG_DEFAULT) {
            if ($this->_save_handler == self::FLAG_FILECACHE) {
                $this->_container = B::i()->mode->cache->engine('file')->pick()->flag('session/');
            } elseif ($this->_save_handler == self::FLAG_MEMCACHED) {
                $this->_container = B::i()->mode->cache->engine('memcached')->pick();
            }
            $this->_container->expire($this->_expire);
            $seted = session_set_save_handler(
                array($this, 'open'), 
                array($this, 'close'), 
                array($this, 'read'), 
                array($this, 'write'), 
                array($this, 'destory'), 
                array($this, 'gc')
            );
            if (!$seted) {
                throw new BException('session_custom_failed');
            }
        }
        is_null($session_id) || session_id($session_id);
        session_start();
	}
	
	/**
	 * 打开session
	 * 
	 * @access public
	 * @param string $save_path
	 * @param string $session_name
	 * @return bool
	 */
	function open($save_path, $session_name)
	{
        return true;
	}
	
	/**
	 * 关闭session控制
	 * 
	 * @access public
	 * @return bool
	 */
	function close()
	{
		return true;
	}
	
	/**
	 * 读取session值
	 * 
	 * @access public
	 * @param string $session_id
	 * @return string
	 */
	function read($session_id)
	{
		return (string)$this->_container->get($session_id);
	}
	
	/**
	 * 设置session值
	 * 
	 * @access public
	 * @param string $session_id
	 * @param string $session_data
	 * @return bool
	 */
	function write($session_id, $session_data)
	{
	    return $this->_container->set($session_id, $session_data);
	}
	
    /**
     * 销毁session值
     * 
     * @access public
     * @param string $session_id
     * @return bool
     */
    function destory($session_id)
    {
    	return $this->_container->instance()->delete($session_id);
    }

	/**
	 * 清理过期缓存
	 * 
	 * @access public
	 * @param int $maxlifetime
	 * @return bool
	 */
	function gc($max_lifetime)
	{

	}

    /**
     * 取session数据
     * 
     * @access public
     * @param string $name
     * @return mixed
     */
    function get($name = null)
    {
        return is_null($name) ? $_SESSION : $_SESSION[$name];
    }
    
    /**
     * 存session数据
     *
     * @access public
     * @param string $name
     * @param mixed $data
     * @return mixed
     */
    function set($name, $data)
    {
        $_SESSION[$name] = $data;
        return $this;
    }
    
    /**
     * 删除某session值
     *
     * @access public
     * @param string $name
     * @return object
     */
    function del($name)
    {
        unset($_SESSION[$name]);
        return $this;
    }
    
    /**
     * 清理session数据
     *
     * @access public
     * @return bool
     */
    function clean()
    {
    	return session_destroy();
    }

    /**
     * 取session数据
     * 
     * @access public
     * @param string $name
     * @return mixed
     */
    function __get($name)
    {
        return $this->get($name);
    }
    
    /**
     * 存session数据
     *
     * @access public
     * @param string $name
     * @param mixed $data
     * @return mixed
     */
    function __set($name, $data)
    {
        return $this->set($name, $data);
    }
	
}

?>
